package training;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class FilmsServlet
 */
public class FilmsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public FilmsServlet() {
		// TODO add print here 
		super();
	}

	// TODO override init() method and place initialization there
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO extract params to constant
		final String parameterName = request.getParameter("Name");
		List<Film> films = getFilms(parameterName);

		StringBuilder outText = new StringBuilder();
		outText.append("<html><body>" );
		outText.append(getThecnicalDetailsInfo(request));
		outText.append(getHtmlList(films));
		outText.append("</body></html>");
		
		final PrintWriter writer = response.getWriter();
		writer.print(outText.toString());
		writer.close();
	}

	private String getThecnicalDetailsInfo(HttpServletRequest request) {
		final String info = "parameter " + "Name" + ": " + request.getParameter("Name");
		// TODO add more info from servlet's methods:  path, some headers, port, method etc
		return info;
	}

	//TODO write correct method
	private String getHtmlList(List<Film> films) {
		final String string = "<ol>" + "  <li> Item one </li>"
				+ "  <li> Item two </li>" + "  </ol>";
		return string;
	}

	private List<Film> getFilms(String parameter) {
		// TODO Auto-generated method stub
		return new ArrayList<Film>();
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		final PrintWriter writer = response.getWriter();
		final String parameter = request.getParameter("Name");
		List<Film> films = getFilms(parameter);

		String outText;

		final String string = getHtmlList(films);
		outText = "<html><body>" + parameter + string + "  </body></html>";

		writer.print(outText);
		writer.close();
	}

}
